﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.UI;
using WinnerCMS.BLL;
using WinnerCMS.BLL.PageFactory;
using WinnerCMS.BLL.PageFactory.TagModule;
using WinnerCMS.Common;
using WinnerCMS.Model;

namespace WinnerCMS.WebFactory
{
    public class ShopShow : PublicPage
    {
        private int _goodsId;
        private int _categoryId;
        private string _key;

        protected Goods model;



        public ShopShow()
        {

        }

        #region 重写事件
        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion



        private void Page_Load(object sender, System.EventArgs e)
        {
            if (int.TryParse(Request.QueryString["ID"], out this._goodsId) == false)
            {
                Response.Write("参数错误"); return;
            }

         

            GoodsManage myIndexShop = new GoodsManage();

            if (myIndexShop == null)
            {
                Response.Write("查无此数据。"); return;
            }

            this.model = myIndexShop.PublicShow(this._goodsId);

            if (this.model == null)
            {
                return;
            }
        }

        #region 内容处理
        private void GoodsContext(StringBuilder TempStrBuilder)
        { 
            this._categoryId = this.model.CategoryId ; 
            this._key = this.model.Tags;

            PageCommon.RepalceFiled(TempStrBuilder, this.model);
        }
        #endregion

        #region 重写Render
        protected override void Render(HtmlTextWriter writer)
        {
            StringBuilder winner = new StringBuilder();
            using (TextWriter tempWriter = new StringWriter())
            {
                base.Render(new HtmlTextWriter(tempWriter));
                winner.Append(tempWriter.ToString());
            }
             
            winner.Replace("@ContentId@", this._goodsId.ToString());
        

            IList<string> matchresult;
              

            matchresult = TagEngine.TagCollection(winner.ToString(), "CommentShow");
            foreach (string nextmatch in matchresult)
            {
                if (this.model.AllowComment )
                {
                    winner.Replace(nextmatch, CommentCommon.CommentShow(nextmatch, this._goodsId ));
                }
                else
                {

                    winner.Replace(nextmatch, string.Empty);
                }
            }
              
            GoodsContext(winner);
             

            CategoryCommon.Path(winner, this._categoryId);
             
            if (Request["Method"] != null)
            {
                string Path = StaticFilePath.ContentShowUrl(this.model);
                new FileIO().WriterFile(HttpServer.MapPath(Path), winner.ToString());
            }
            else
            {
                writer.Write(winner.ToString());
            }
        }
        #endregion
    }
}